//
//  GoodsTypeChooseViewController.m
//  阿帕
//
//  Created by mac on 2020/1/7.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "GoodsTypeChooseViewController.h"

@interface GoodsTypeChooseViewController ()
{
    NSArray *_subTagArr;
    NSInteger _parentTagViewHeight;
}
@property (strong , nonatomic)NSMutableArray *dataArray;

@property (strong , nonatomic)NSMutableArray *tagCodeArray;

@property (strong , nonatomic)NSMutableArray *tagNameArray;

@property (strong , nonatomic)UIView *subTagBtnView;

@property (strong , nonatomic)UIButton *addCardButton;

@end

@implementation GoodsTypeChooseViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _parentTagViewHeight = 0;
    
    self.navigationItem.title = @"选择货物类型";
    
    self.view.backgroundColor = BackgroundColor;
    //标题
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 120, 50)];
    textLabel.text = @"* 点击选中";
    textLabel.textColor = Theme_ColorValue;
    textLabel.font = font(15);
    textLabel.adjustsFontSizeToFitWidth = YES;
    [self.view addSubview:textLabel];
    
    [self goodsTypeListRequest];
 
    [self.view addSubview:self.addCardButton];

}

- (void)setupBaseView {
    
    CGFloat tagBtnX = 16;
    CGFloat tagBtnY = 60;
    
    for (int i=0; i<self.dataArray.count; i++) {
    
        GoodsTypeModel *model = [self.dataArray objectAtIndex:i];
        
        CGSize tagTextSize = [model.name sizeWithFont:font(12) maxSize:CGSizeMake(ScreenW-32-32, 30)];

        if (tagBtnX+tagTextSize.width+30 > ScreenW-32) {
            
            tagBtnX = 16;
            tagBtnY += 30+15;
        }
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(tagBtnX, tagBtnY, tagTextSize.width+30, 25);
        button.tag = 100 + i;
        cutBorder(button, 1, @"#96A0B9");
        [button setTitle:model.name forState:UIControlStateNormal];
        button.titleLabel.font = font(12);
        [button setTitleColor:Light_TextColor forState:UIControlStateNormal];
        [button addTarget:self action:@selector(mainTagbtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        
        tagBtnX = CGRectGetMaxX(button.frame)+10;
    }
    
    _parentTagViewHeight = tagBtnY;
}

//主分类点击事件
- (void)mainTagbtnClick:(UIButton *)btn {
    
    [self.tagCodeArray removeAllObjects];
    [self.tagNameArray removeAllObjects];
    
    for (int i=0; i<self.dataArray.count; i++) {
        if (i == btn.tag-100) {
            [btn setTitleColor:Theme_ColorValue forState:UIControlStateNormal];
            cutBorder(btn, 1, @"#5e8bf7");
            
            GoodsTypeModel *model = [self.dataArray objectAtIndex:btn.tag-100];
            
            if ([DCSpeedy isBlankArr:model.children] == NO) {
                
                [self createSubTagBtn:model.children];
                
            } else {
                if (_subTagBtnView) {
                    [_subTagBtnView removeAllSubviews];
                }
                
                [self.tagCodeArray addObject:model.code];
                [self.tagNameArray addObject:model.name];
            }
            
        } else {
            UIButton *otherBtn = [self.view viewWithTag:100 + i];
            [otherBtn setTitleColor:Light_TextColor forState:UIControlStateNormal];
            cutBorder(otherBtn, 1, @"#96A0B9");
        }
    }
}

//子分类点击事件
- (void)subTagbtnClick:(UIButton *)btn {
        
    ChildrenItem *model = [_subTagArr objectAtIndex:btn.tag - 200];
    
    btn.selected = !btn.selected;
    if (btn.selected) {
        [btn setTitleColor:Theme_ColorValue forState:UIControlStateNormal];
        cutBorder(btn, 1, @"#5e8bf7");
        
        [self.tagCodeArray addObject:model.code];
        [self.tagNameArray addObject:model.name];
        
    } else {
        [btn setTitleColor:Light_TextColor forState:UIControlStateNormal];
        cutBorder(btn, 1, @"#96A0B9");
        
        NSInteger codeIndex = [self.tagCodeArray indexOfObject:model.code];
        [self.tagCodeArray removeObjectAtIndex:codeIndex];
        
        NSInteger nameIndex = [self.tagNameArray indexOfObject:model.name];
        [self.tagNameArray removeObjectAtIndex:nameIndex];
    }
    
}

- (void)confirmEventClick {
    
    NSString *codes = [self.tagCodeArray componentsJoinedByString:@","];
    NSString *names = [self.tagNameArray componentsJoinedByString:@","];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(GetAllGoodsCodeString::)]) {
        [self.delegate GetAllGoodsCodeString:codes :names];
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)createSubTagBtn:(NSArray *)array {
    
    _subTagArr = array;
    
    if (_subTagBtnView) {
        [_subTagBtnView removeAllSubviews];
    }
    
    CGFloat tagBtnX = 16;
    CGFloat tagBtnY = 0;
    
    for (int i=0; i<array.count; i++) {
        
        ChildrenItem *model = [array objectAtIndex:i];
        
        CGSize tagTextSize = [model.name sizeWithFont:font(12) maxSize:CGSizeMake(ScreenW-32-32, 30)];
        
        if (tagBtnX+tagTextSize.width+30 > ScreenW-32) {
            
            tagBtnX = 16;
            tagBtnY += 30+15;
        }
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(tagBtnX, tagBtnY, tagTextSize.width+30, 25);
        button.tag = 200 + i;
        cutBorder(button, 1, @"#96A0B9");
        [button setTitle:model.name forState:UIControlStateNormal];
        button.titleLabel.font = font(12);
        [button setTitleColor:Light_TextColor forState:UIControlStateNormal];
        [button addTarget:self action:@selector(subTagbtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.subTagBtnView addSubview:button];
        
        tagBtnX = CGRectGetMaxX(button.frame)+10;
    }
}

- (void)goodsTypeListRequest {
    WEAKSELF
    NSDictionary *parmas = @{
        @"parentCode": @"goodsBigType",
        @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
    };
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], GOODSTYPE) withParameter:parmas withVC:self netName:@"货物类型" success:^(id response, NSString *msg) {
        
        if ([response[@"records"] count] != 0) {
            
            [weakSelf.dataArray addObjectsFromArray:[GoodsTypeModel mj_objectArrayWithKeyValuesArray:response[@"records"]]];
            
            [weakSelf setupBaseView];
        }
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
    
}


- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (NSMutableArray *)tagCodeArray {
    if (!_tagCodeArray) {
        _tagCodeArray = [[NSMutableArray alloc] init];
    }
    return _tagCodeArray;
}

- (NSMutableArray *)tagNameArray {
    if (!_tagNameArray) {
        _tagNameArray = [[NSMutableArray alloc] init];
    }
    return _tagNameArray;
}

- (UIView *)subTagBtnView {
    if (!_subTagBtnView) {
        _subTagBtnView = [[UIView alloc] initWithFrame:CGRectMake(0, _parentTagViewHeight + 50, ScreenW, 200)];
        _subTagBtnView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_subTagBtnView];
    }
    return _subTagBtnView;
}
- (UIButton *)addCardButton {
    if (!_addCardButton) {
        _addCardButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addCardButton setTitle:@"确定" forState:UIControlStateNormal];
        _addCardButton.titleLabel.font = font(16);
        [_addCardButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_addCardButton setBackgroundColor:Theme_ColorValue];
        [_addCardButton addTarget:self action:@selector(confirmEventClick) forControlEvents:UIControlEventTouchUpInside];
        _addCardButton.frame = CGRectMake(0, ScreenH - NavHeight - MenuBarHeight - 45, ScreenW, 45);
    }
    return _addCardButton;
}
//- (void)createUI
//{
//    NSArray * tagArr = [NSArray arrayWithObjects:@"时间机器",@"世界大战",@"机器人",@"2001：太空奥德赛",@"银河系漫游指南",@"沙丘",@"隐形人",@"海底两万里",@"三体",@"华氏451度",@"火星编年史",@"球状闪电",@"科幻",@"当夜空吞噬星光的时候",@"寂",@"神经漫游者",@"与吾同在",@"北京折叠",@"幻想曲",@"地心漫游记",@"环游地球80天",@"寻",@"星球大战系列多部曲", nil];
//    CGFloat tagBtnX = 16;
//    CGFloat tagBtnY = 150;
//    for (int i= 0; i<tagArr.count; i++) {
//
//        CGSize tagTextSize = [tagArr[i] sizeWithFont:font(14) maxSize:CGSizeMake(ScreenW-32-32, 30)];
//        if (tagBtnX+tagTextSize.width+30 > ScreenW-32) {
//
//            tagBtnX = 16;
//            tagBtnY += 30+15;
//        }
//        UIButton * tagBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        tagBtn.tag = 100+i;
//        tagBtn.frame = CGRectMake(tagBtnX, tagBtnY, tagTextSize.width+30, 30);
//        [tagBtn setTitle:tagArr[i] forState:UIControlStateNormal];
//        [tagBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
//        [tagBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
//        tagBtn.titleLabel.font = font(14);
//        tagBtn.layer.cornerRadius = 15;
//        tagBtn.layer.masksToBounds = YES;
//        tagBtn.layer.borderWidth = 1;
//        tagBtn.layer.borderColor = [UIColor orangeColor].CGColor;
////        [tagBtn addTarget:self action:@selector(tagBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//        [self.view addSubview:tagBtn];
//
//        tagBtnX = CGRectGetMaxX(tagBtn.frame)+10;
//    }
//}

@end


